home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / p_tapr / tnchst / aft.c next >
Text File  |  1992-03-16  |  9KB  |  362 lines

  1. /*****************************************
  2.  
  3.    AFT driver  (c) 1991 Howard Goldstein
  4.  
  5. Permission for non-commercial Amateur Radio
  6. use granted 
  7.  
  8. ******************************************/
  9.  
  10. #define    DEBUG   1
  11. /* dumps packets to printf */
  12.  
  13.    /* set to 1 when running under uSoft C */
  14. #define   MSDOS   1
  15.  
  16. #if MSDOS
  17. #include "aft.inc"
  18. #endif
  19.  
  20.  
  21. #define   AFTMAXFRAMESIZE 350
  22.  
  23. #include   "blp.inc"
  24.  
  25. /** enable XON/XOFF transparancy **/
  26. #define    AFTLEVEL1 1
  27.  
  28. struct itm_struct
  29. {
  30.     char    *itm_src    ;        /* this is the sending task */
  31.     char    *itm_dest    ;        /*  "   "   "  dest task */
  32.     char     itm_ctrl    ;        /* message type */
  33.     char    *itm_msg       ;        /* pointer to message */
  34.                         /* used in AFTRCV to hold
  35.                            cksum temporarily I hope */
  36.     unsigned int msg_len    ;        /* length of message */
  37.     char    itm_data[1]    ;        /* optional area begins
  38.                            here.  allows
  39.                            buffers to live in mem
  40.                            contigious to an itm
  41.                            structure.  an n byte
  42.                            data field could be started
  43.                            at this offset IF PROPER
  44.                            allocation is performed! */
  45. };
  46.  
  47.  
  48.  
  49.  
  50.     /* statics for exceedingly simple aft rx driver */
  51.  
  52. struct    itm_struct    *aftr_msg;        /* when 0, there is no
  53.                            current buffer ready.
  54.                           when !0, then the struct
  55.                           is ready to  rcv the next
  56.                          aft frame  */
  57.  
  58.     word    aftrstate, aftrlen ;
  59.                     /* aft rx machine state, and length
  60.                        at current instant
  61.                        if in a receive state */
  62.  
  63.     byte    *aftrptr;        /* pointer used by driver to quickly
  64.                        insert the char in the rcvd msg */
  65.  
  66.     byte    aftrcksum ;        /* current receive cksum */
  67.  
  68. void cdecl aftr_init()
  69. {
  70.     aftr_msg = (struct itm_struct *) NULL;
  71.                     /* null out so no message is waiting*/
  72.     aftrstate = AFRHUNT;        /* state == hunting for flags */
  73. }
  74.  
  75.  
  76.  
  77.     /* runs from executive, if !MSDOS, else is called from char-by-char
  78.       driver if MSDOS.  processes the AFT message */
  79.  
  80. int cdecl
  81. aftr_handler( msg )
  82. struct itm_struct *msg;
  83. {
  84.  
  85. #if MSDOS
  86.  
  87.     byte    *c;
  88.     byte    cksum;
  89.     word    len;
  90.     extern   void     cdecl aft_in_handler();
  91.     struct bframe_struct *bframe;    /* blp frame pointer */
  92.  
  93.     if ( msg->msg_len < 1 )
  94.         return( FALSE ) ;        /* error - too short for
  95.                            AFT */
  96.  
  97.     /** rest is debug ***/
  98.  
  99.     bframe = (struct bframe_struct *) msg->itm_data+2 ;
  100.                 /* points to blp frame */
  101.  
  102.     if ( bframe->lcn == BROADCAST_LCN )    /* if bdcst from tnc */
  103.         monframe(msg->itm_data+2,msg->msg_len-2); /* display the frame */
  104.        /*** note -  ought to do above in blp itself... */
  105.  
  106. #if AFT_FRAME_TRACE
  107.    /* take out for now if 0*/
  108.         /* just display to console */
  109.     c = msg->itm_data;    /* set ptr */
  110.  
  111.     cksum =  (byte) msg->itm_msg;
  112.  
  113. #if BROADCAST_TRACE
  114.     {
  115. #else
  116.     if ( *c != 127 )    /* not bcast lcn? */
  117.     {
  118. #endif
  119.      printf("received frame len %d checksum %02d DLC cmd %02X lcn %d cmd $%02X\n",
  120.                 msg->msg_len ,
  121.           cksum, *c, *(c+1),*(c+2));
  122.  
  123.     len = msg->msg_len - 1 ;        /* take off dlc cmd above */
  124.     c += 1;
  125.  
  126.     while ( len-- )
  127.     {
  128.            printf("%02X,'%c'|", *c, ( *c >= 0x20 ? *c: 0x20 ) );
  129.         ++c;
  130.     }
  131.  
  132.     putchar('\n');
  133.     }
  134. #endif
  135.  
  136.             /* send it to the in handler */
  137.     aft_in_handler( (struct dlc_frame_struct *) msg->itm_data , msg->msg_len );
  138.  
  139.     free( (byte *) msg );
  140.  
  141. #else
  142.     ;   /* no function yet for TNC */
  143. #endif
  144. }
  145.  
  146.  
  147.     /* char-by-character processing */
  148. #if MSDOS
  149.  
  150. aftr_proc( c )
  151. byte c;
  152. {
  153.     byte    frame_cksum ;        /* holds cksum found in rcvd frame */
  154.  
  155.     switch ( aftrstate )        /* handle according to current state */
  156.     {
  157.         case AFRHUNT:        /* hunting for a sync? */
  158.             if ( c == AFT_FLAG )    /* if it's a sync then */
  159.                 aftrstate = AFRFLAG ;    /* wait for non-flag */
  160.             break ;
  161.         case AFRFLAG:        /* hunting for !sync? */
  162.             if ( c == AFT_FLAG )    /* if sync then */
  163.                 break ;        /* ignore */
  164.             else
  165.             {
  166.                 /* 1st char of new frame here.  first, find
  167.                    an itm to use */
  168.                 if ( !aftr_msg )    /*need to find an itm? */
  169.                 {
  170.                         /* allocate a big enuf buffer
  171.                            for an intertask msg */
  172.                     if ( ! (aftr_msg = (struct itm_struct *)
  173.                         malloc(AFTMAXFRAMESIZE +
  174.                          sizeof (struct itm_struct)) ) )
  175.                     {    /*true if could not get a new buf*/
  176.                         aftrstate = AFRHUNT ;
  177.                         break ;   /** break out of
  178.                                   the big switch **/
  179.                     }
  180.                 }
  181.                     /* now have an itm ready*/
  182.                     /* set cksum and count to 0 */
  183.                 aftrlen = 0 ;
  184.                 aftrcksum = 0 ;
  185.  
  186.                 aftrptr = aftr_msg->itm_data ;
  187.                     /* data ptr at bgng of buffer */
  188.                 aftrstate = AFRRX;
  189.             }
  190.             /** and fall through to DATA state **/
  191.                 case AFRRX:            /* basic data state */
  192.  
  193.                 /* first see whether the max frame size
  194.                    has been reached.  if so, state>hunt
  195.                    effectively discards the frame */
  196.             if ( aftrlen >= AFTMAXFRAMESIZE )
  197.                   aftrstate = AFRHUNT ;
  198.                else
  199.             switch ( c )        /* according to char rcvd */
  200.             {
  201.                 case AFT_DLE:        /* escape? */
  202.                     aftrstate = AFRRX2;    /* yes, state->
  203.                                    escape hdlr*/
  204.                     break ;
  205.                 case AFT_FLAG:        /* end of frame? */
  206.                     if ( (aftr_msg->msg_len = aftrlen-1) > 0 )
  207.                         /* store msg len.  'IF len > 0 */
  208.                     {
  209.                           frame_cksum = *(aftrptr-1) ;
  210.                                                    /* remove value in place
  211.                               cksum byte will be,
  212.                                                       from cksum calc*/
  213.                     /*    aftrcksum -= frame_cksum ; */
  214.  
  215.                         aftr_msg->itm_msg = (byte *)
  216.                           aftrcksum ;
  217.                             /* REMOVE LATER -
  218.                                  for now store byte
  219.                              cksum in itm_msg field..
  220.                           cast needed;itm_msg is a ptr*/
  221.  
  222.  
  223.                         /** test for cksum valid **/
  224.                             /* if good checksum */
  225.                         if ( !aftrcksum )
  226.                         {
  227. #if MSDOS
  228.                          if ( aftr_handler( aftr_msg ) )
  229.                       /* release msg in non-dispatched
  230.                          msdos version if aftr handler
  231.                          ran ok */
  232.                             aftr_msg = (struct
  233.                              itm_struct *) NULL ;
  234.  
  235. #else
  236.                          if ( q_task( aftr_handler,
  237.                             aftr_msg, INT1 ) )
  238.                           err ; /* error - must decide
  239.                         what to do when can't queue
  240.                         a task to service this */
  241. #endif
  242.                         }
  243.                         else   /* bad checksum */
  244.                             ; /* nop */
  245.                     }
  246.                                         else ;
  247.                       /* else nop 'cuz length <1 (i.e.
  248.                       less than 1 data and 1 cksum byte):
  249.                            ignore these frames */
  250.  
  251.  
  252.                     aftrstate = AFRFLAG;
  253.                     break;
  254.                 default:        /* reg char */
  255.                     aftrcksum += ( *aftrptr++ = c ) ;
  256.                     ++aftrlen;
  257.             }
  258.                         break;
  259.         case AFRRX2:
  260.             /** previous character received was AFT_DLE **/
  261.  
  262.                 /** process aft abort **/
  263.             if ( c == AFT_FLAG )    /* if flag occurs after DLE */
  264.                 aftrstate = AFRFLAG ;    /* then change state
  265.                         ( aborts the frame being rcvd */
  266.             else
  267.             {
  268.                 /** else this character is xor'd info **/
  269.                 aftrcksum += ( *aftrptr++ = ( c ^ 0x20 ) ) ;
  270.                 ++aftrlen;
  271.                 aftrstate = AFRRX;
  272.             }
  273.             break;
  274.     }
  275. }
  276.  
  277. #endif
  278.  
  279. /*** TRANSMIT PROCEDURES ***/
  280. void aftt_send_bin( ch )        /* send char to async line transprntly*/
  281. byte ch;
  282. {
  283.     PUTAUX( ch );            /** customize for installation **/
  284. }
  285.  
  286. void aftt_send_data( ch )        /* send char, using transparancy */
  287. byte ch;
  288. {
  289.     switch (ch)            /* see what we've got here */
  290.     {
  291.             /* basic l0 transparancy for flag & escape */
  292.         case AFT_FLAG: case AFT_DLE:
  293. #if AFTLEVEL1
  294.             /* level 1 transparancy for xon/xoff */
  295.         case XON: case XOFF:
  296. #endif
  297.  
  298.                             /* first, */
  299.             aftt_send_bin( AFT_DLE );    /* send escape char */
  300.  
  301.             ch ^= 0x20 ;    /* then xor the char to convert it */
  302.             /* then fall through to default to send xvtd char */
  303.         default:
  304.             aftt_send_bin( ch );    /* send the resulting */
  305.             break;
  306.     }
  307. }
  308. int cdecl aftt_send( msg )   /* enter with itm; returns 0 if err, !0 if ok */
  309. struct itm_struct *msg;
  310. {
  311.     byte    cksum, *dptr;        /* checksum accum, ptr to data */
  312.     word    len ;
  313.     int    retstat     ;        /* return code */
  314.     byte    *c    ;        /* frame tracing*/
  315.  
  316.     len = msg->msg_len ;        /* length of data in itm */
  317.  
  318.     if ( len > 0 )            /* if more than 0 bytes */
  319.     {
  320.         cksum = 0 ;        /* initialize cksum */
  321.         dptr = msg->itm_data ;    /* init ptr to 1st data byte in msg */
  322.  
  323.         aftt_send_bin( AFT_FLAG ) ;   /* send opening flag */
  324.  
  325.         while ( len-- > 0 )    /* while characters to send */
  326.         {
  327.             cksum += *dptr ;    /* add value to cksum accum */
  328.             aftt_send_data( *dptr++) ; /* and send the char */
  329.         }
  330.  
  331.         aftt_send_data( -cksum  );    /* now send cksum byte */
  332.         aftt_send_bin( AFT_FLAG );    /* and closing flag */
  333.         retstat = TRUE ;
  334. #if AFT_FRAME_TRACE
  335.    /* take out for now if 0*/
  336.         /* just display to console */
  337.     c = msg->itm_data;    /* set ptr */
  338.  
  339.     printf("sent frame len %d  DLC %02X lcn %d cmd $%02X\n",
  340.                 msg->msg_len ,
  341.           *c, *(c+1),*(c+2));
  342.  
  343.     len = msg->msg_len - 1 ;        /* take off DLC cmd */
  344.     c += 1;
  345.  
  346.     while ( len-- )
  347.     {
  348.            printf("%02X,'%c'|", *c, ( *c >= 0x20 ? *c: 0x20 ) );
  349.         ++c;
  350.     }
  351.  
  352.     putchar('\n');
  353. #endif
  354.  
  355.  
  356.     }
  357.     else /* error - datalen was 0 */
  358.         retstat = FALSE ;
  359.  
  360.     return( retstat );
  361. }
  362.